package cn.blunce.LinkList;

/**
 * Created by BlueSmile on 2016/3/23 0023.
 */
public class LinkList {
    private Node head;

    LinkList() {
        head = new Node(0);
    }

    public int getLength() {
        return head.value;
    }

    public void addIndexNode(int index, int val) {
        /**
         * index from 0 to n-1(length n)
         */
        int cur_index = 0;
        Node point = head.next;
        while (cur_index != index) {
            point = point.next;
            cur_index++;
        }
        Node new_node = new Node(val);
        new_node.next = point.next;
        point.next = new_node;
        head.value++;
    }

    public void deleteIndexNode(int index) {
        /**
         * index from 0 to n-1(length n)
         */
        if (index == 0) {
            head.next = head.next.next;
        } else {
            int cur_index = 0;
            Node point = head.next;
            while (cur_index != index - 1) {
                point = point.next;
                cur_index++;
            }
            point.next = point.next.next;
        }
        head.value--;
    }
}
